<template>
  <div class="app-container">
    <!-- 顶部控制台 -->
    <div class="filter-container">
      <el-input class="filter-item" style="width: 200px; margin-left: 0px" size="mini" placeholder="请输入搜索的文案标题" v-model="form.searchTitle"/>
      <el-select class="filter-item" style="width: 160px" size="mini" v-model="form.searchClass" @change="handleSearchClass">
        <el-option v-for="item in pageData.searchClassList" :key="item.value" :label="item.label" :value="item.value"/>
      </el-select>
      <el-select class="filter-item" style="width: 160px" size="mini" v-model="form.searchType" :disabled="pageData.searchTypeStatus" @change="handlePageInit">
        <el-option v-for="item in pageData.searchTypeList" :key="item.value" :label="item.label" :value="item.value"/>
      </el-select>
      <el-select class="filter-item" style="width: 120px" size="mini" v-model="form.searchVipStatus" @change="handlePageInit">
        <el-option v-for="item in pageData.searchVipStatusList" :key="item.value" :label="item.label" :value="item.value"/>
      </el-select>
      <el-select class="filter-item" style="width: 120px" size="mini" v-model="form.searchStatus" @change="handlePageInit">
        <el-option v-for="item in pageData.searchStatusList" :key="item.value" :label="item.label" :value="item.value"/>
      </el-select>
      <el-button type="primary" size="mini" icon="el-icon-search" @click="handlePageInit">搜索</el-button>
      <el-button type="primary" size="mini" icon="el-icon-edit" @click="handleOpenSavePage">添加</el-button>
      <el-button type="success" size="mini" icon="el-icon-share" @click="handleBatchReleaseData" :disabled="form.selection.length < 1">批量发布</el-button>
    </div>
    <!-- 页面数据列表 -->
    <div class="data-table">
      <el-table :data="responseData.tableList"
                border style="width: 100%;"
                v-loading="pageData.loading"
                element-loading-text="拼命加载中"
                element-loading-spinner="el-icon-loading"
                element-loading-background="rgba(0, 0, 0, 0.8)"
                :header-cell-style="{ background: '#545C64', color: '#ffffff' }"
                @selection-change="handleTableSelection">
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column fixed="left" prop="articleId" label="文案ID" width="320" align="center"/>
        <el-table-column prop="articleTitle" label="文案标题" width="320" align="center">
          <template slot-scope="scope">
            <span class="link-type" @click="handleOpenEditPage(scope)">{{ scope.row.articleTitle }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="articleClassId" label="所属板块" width="150" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.articleClassId === 1"><el-tag type="success">案例点评版块</el-tag></span>
            <span v-if="scope.row.articleClassId === 4"><el-tag type="danger">法律法规版块</el-tag></span>
            <span v-if="scope.row.articleClassId === 41"><el-tag type="primary">法律</el-tag></span>
            <span v-if="scope.row.articleClassId === 42"><el-tag type="success">法规</el-tag></span>
            <span v-if="scope.row.articleClassId === 43"><el-tag type="info">部门规章</el-tag></span>
            <span v-if="scope.row.articleClassId === 44"><el-tag type="warning">规范性文件</el-tag></span>
            <span v-if="scope.row.articleClassId === 4401"><el-tag type="danger">国务院规范性文件</el-tag></span>
            <span v-if="scope.row.articleClassId === 4402"><el-tag type="primary">财政部规范性文件</el-tag></span>
            <span v-if="scope.row.articleClassId === 4403"><el-tag type="success">中央部委规范性文件</el-tag></span>
            <span v-if="scope.row.articleClassId === 4404"><el-tag type="danger">国家发展和改革委员会规范性文件</el-tag></span>
            <span v-if="scope.row.articleClassId === 4405"><el-tag type="primary">教育部规范性文件</el-tag></span>
            <span v-if="scope.row.articleClassId === 4406"><el-tag type="info">工业和信息化部规范性文件</el-tag></span>
            <span v-if="scope.row.articleClassId === 4407"><el-tag type="warning">公安部规范性文件</el-tag></span>
            <span v-if="scope.row.articleClassId === 4408"><el-tag type="danger">商务部规范性文件</el-tag></span>
            <span v-if="scope.row.articleClassId === 4409"><el-tag type="primary">交通运输部规范性文件</el-tag></span>
            <span v-if="scope.row.articleClassId === 4410"><el-tag type="success">住房和城乡建设部规范性文件</el-tag></span>
            <span v-if="scope.row.articleClassId === 45"><el-tag type="info">地方法规规章</el-tag></span>
            <span v-if="scope.row.articleClassId === 4501"><el-tag type="warning">北京市</el-tag></span>
            <span v-if="scope.row.articleClassId === 4502"><el-tag type="danger">天津市</el-tag></span>
            <span v-if="scope.row.articleClassId === 4503"><el-tag type="primary">上海市</el-tag></span>
            <span v-if="scope.row.articleClassId === 4504"><el-tag type="success">重庆市</el-tag></span>
            <span v-if="scope.row.articleClassId === 4505"><el-tag type="info">河北省</el-tag></span>
            <span v-if="scope.row.articleClassId === 4506"><el-tag type="warning">山西省</el-tag></span>
            <span v-if="scope.row.articleClassId === 4507"><el-tag type="danger">台湾地区</el-tag></span>
            <span v-if="scope.row.articleClassId === 4508"><el-tag type="primary">辽宁省</el-tag></span>
            <span v-if="scope.row.articleClassId === 4509"><el-tag type="success">吉林省</el-tag></span>
            <span v-if="scope.row.articleClassId === 4510"><el-tag type="info">黑龙江省</el-tag></span>
            <span v-if="scope.row.articleClassId === 4511"><el-tag type="warning">江苏省</el-tag></span>
            <span v-if="scope.row.articleClassId === 4512"><el-tag type="danger">浙江省</el-tag></span>
            <span v-if="scope.row.articleClassId === 4513"><el-tag type="primary">安徽省</el-tag></span>
            <span v-if="scope.row.articleClassId === 4514"><el-tag type="success">福建省</el-tag></span>
            <span v-if="scope.row.articleClassId === 4515"><el-tag type="info">江西省</el-tag></span>
            <span v-if="scope.row.articleClassId === 4516"><el-tag type="warning">山东省</el-tag></span>
            <span v-if="scope.row.articleClassId === 4517"><el-tag type="danger">河南省</el-tag></span>
            <span v-if="scope.row.articleClassId === 4518"><el-tag type="primary">湖北省</el-tag></span>
            <span v-if="scope.row.articleClassId === 4519"><el-tag type="success">湖南省</el-tag></span>
            <span v-if="scope.row.articleClassId === 4520"><el-tag type="info">广东省</el-tag></span>
            <span v-if="scope.row.articleClassId === 4521"><el-tag type="warning">海南省</el-tag></span>
            <span v-if="scope.row.articleClassId === 4522"><el-tag type="danger">四川省</el-tag></span>
            <span v-if="scope.row.articleClassId === 4523"><el-tag type="primary">贵州省</el-tag></span>
            <span v-if="scope.row.articleClassId === 4524"><el-tag type="success">云南省</el-tag></span>
            <span v-if="scope.row.articleClassId === 4525"><el-tag type="info">陕西省</el-tag></span>
            <span v-if="scope.row.articleClassId === 4526"><el-tag type="warning">甘肃省</el-tag></span>
            <span v-if="scope.row.articleClassId === 4527"><el-tag type="danger">青海省</el-tag></span>
            <span v-if="scope.row.articleClassId === 4528"><el-tag type="primary">内蒙古自治区</el-tag></span>
            <span v-if="scope.row.articleClassId === 4529"><el-tag type="success">广西壮族自治区</el-tag></span>
            <span v-if="scope.row.articleClassId === 4530"><el-tag type="info">西藏自治区</el-tag></span>
            <span v-if="scope.row.articleClassId === 4531"><el-tag type="warning">宁夏回族自治区</el-tag></span>
            <span v-if="scope.row.articleClassId === 4532"><el-tag type="primary">新疆维吾尔族自治区</el-tag></span>
            <span v-if="scope.row.articleClassId === 4533"><el-tag type="danger">香港</el-tag></span>
            <span v-if="scope.row.articleClassId === 4534"><el-tag type="success">澳门</el-tag></span>
            <span v-if="scope.row.articleClassId === 46"><el-tag type="primary">热门法规</el-tag></span>
            <span v-if="scope.row.articleClassId === 4601"><el-tag type="success">PPP项目</el-tag></span>
            <span v-if="scope.row.articleClassId === 4602"><el-tag type="info">政府购买服务</el-tag></span>
            <span v-if="scope.row.articleClassId === 47"><el-tag type="warning">国际法规</el-tag></span>
            <span v-if="scope.row.articleClassId === 4701"><el-tag type="danger">世行规则</el-tag></span>
            <span v-if="scope.row.articleClassId === 4702"><el-tag type="primary">亚行规则</el-tag></span>
            <span v-if="scope.row.articleClassId === 4703"><el-tag type="success">欧盟规则</el-tag></span>
            <span v-if="scope.row.articleClassId === 4704"><el-tag type="info">联合国规则</el-tag></span>
            <span v-if="scope.row.articleClassId === 4705"><el-tag type="warning">美国规则</el-tag></span>
            <span v-if="scope.row.articleClassId === 4706"><el-tag type="danger">英国规则</el-tag></span>
            <span v-if="scope.row.articleClassId === 4707"><el-tag type="success">日本规则</el-tag></span>
            <span v-if="scope.row.articleClassId === 4708"><el-tag type="info">韩国规则</el-tag></span>
            <span v-if="scope.row.articleClassId === 4709"><el-tag type="warning">澳大利亚规则</el-tag></span>
            <span v-if="scope.row.articleClassId === 4710"><el-tag type="primary">加拿大规则</el-tag></span>
            <span v-if="scope.row.articleClassId === 88"><el-tag type="success">新闻版块</el-tag></span>
          </template>
        </el-table-column>
        <el-table-column prop="articleType" label="所属类型" width="120" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.articleType === 1"><el-tag type="success">文字类型</el-tag></span>
            <span v-if="scope.row.articleType === 2"><el-tag type="primary">音频类型</el-tag></span>
            <span v-if="scope.row.articleType === 10"><el-tag type="danger">图集类型</el-tag></span>
          </template>
        </el-table-column>
        <el-table-column prop="articleIsVip" label="付费状态" width="80" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.articleIsVip === 0"><el-tag type="warning">否</el-tag></span>
            <span v-if="scope.row.articleIsVip === 1"><el-tag type="success">是</el-tag></span>
          </template>
        </el-table-column>
        <el-table-column prop="articleStatus" label="审核状态" width="80" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.articleStatus === 0"><el-tag type="warning">下架</el-tag></span>
            <span v-if="scope.row.articleStatus === 1"><el-tag type="success">发布</el-tag></span>
          </template>
        </el-table-column>
        <el-table-column prop="articleClickCount" label="点击量" width="100" align="center"/>
        <el-table-column prop="articleCreateTime" label="创建时间" width="160" align="center"/>
        <el-table-column prop="articleReleaseTime" label="发布时间" width="160" align="center"/>
        <el-table-column fixed="right" label="操作" width="220" align="center">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" @click="handleOpenEditPage(scope)">编辑</el-button>
            <el-button v-if="scope.row.articleStatus === 0" size="mini" type="success" @click="handleUpdateDataStatus(scope, 1)">发布</el-button>
            <el-button v-if="scope.row.articleStatus === 1" size="mini" type="warning" @click="handleUpdateDataStatus(scope, 0)">下架</el-button>
            <el-button size="mini" type="danger" @click="handleDeleteData(scope)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!--分页器-->
    <div style="margin-top: 20px">
      <div class="block" style="width: 100%; text-align: center">
        <el-pagination
          layout="total, sizes, prev, pager, next, jumper"
          :current-page="form.pageNum"
          :page-size="form.pageSize"
          :total="form.total"
          :page-sizes="[12, 50, 100, 200]"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange">
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
  // 组件引入
  // 方法工具引入
  import { PermissionsToDetectFormUser } from '@/utils/PermissionsDetect';
  import { getPermission } from '@/utils/UserCookies';
  // 接口引入
  import { loadCmsArticleList,batchReleaseCmsArticleData,deleteCmsArticleData } from "@/api/content";
  export default {
    name: "article",
    //import引入的组件需要注入到对象中才能使用
    components: {},
    data (){
      return {
        pageData:{
          searchClassList: [
            {label: "全部分类", value: -1},
            {label: "案例点评", value: 1},
            {label: "法律法规", value: 4},
            {label: "新闻", value: 88}
          ],// 一级分类预设信息
          searchTypeStatus: false,// 二级分类是否可选标识
          searchTypeList: [
            {label: "全部类型", value: -1},
            {label: "法律", value: 41},{label: "法规", value: 42},{label: "部门规章", value: 43},
            {label: "规范性文件", value: 44},{label: "国务院规范性文件", value: 4401},{label: "财政部规范性文件", value: 4402},
            {label: "中央部委规范性文件", value: 4403},
            {label: "国家发展和改革委员会规范性文件", value: 4404},{label: "教育部规范性文件", value: 4405},{label: "工业和信息化部规范性文件", value: 4406},
            {label: "公安部规范性文件", value: 4407},{label: "商务部规范性文件", value: 4408},{label: "交通运输部规范性文件", value: 4409},
            {label: "住房和城乡建设部规范性文件", value: 4410},
            {label: "地方法规规章", value: 45},{label: "北京市", value: 4501},{label: "天津市", value: 4502},{label: "上海市", value: 4503},
            {label: "重庆市", value: 4504},{label: "河北省", value: 4505},{label: "山西省", value: 4506},{label: "台湾地区", value: 4507},
            {label: "辽宁省", value: 4508},{label: "吉林省", value: 4509},{label: "黑龙江省", value: 4510},{label: "江苏省", value: 4511},
            {label: "浙江省", value: 4512},{label: "安徽省", value: 4513},{label: "福建省", value: 4514},{label: "江西省", value: 4515},
            {label: "山东省", value: 4516},{label: "河南省", value: 4517},{label: "湖北省", value: 4518},{label: "湖南省", value: 4519},
            {label: "广东省", value: 4520},{label: "海南省", value: 4521},{label: "四川省", value: 4522},{label: "贵州省", value: 4523},
            {label: "云南省", value: 4524},{label: "陕西省", value: 4525},{label: "甘肃省", value: 4526},{label: "青海省", value: 4527},
            {label: "内蒙古自治区", value: 4528},{label: "广西壮族自治区", value: 4529},{label: "西藏自治区", value: 4530},{label: "宁夏回族自治区", value: 4531},
            {label: "新疆维吾尔族自治区", value: 4532},{label: "香港", value: 4533},{label: "澳门", value: 4534},
            {label: "热门法规", value: 46},{label: "PPP项目", value: 4601},{label: "政府购买服务", value: 4602},
            {label: "国际规则", value: 47},{label: "世行规则", value: 4701},{label: "亚行规则", value: 4702},{label: "欧盟规则", value: 4703},
            {label: "联合国规则", value: 4704},{label: "美国规则", value: 4705},{label: "英国规则", value: 4706},{label: "日本规则", value: 4707},
            {label: "韩国规则", value: 4708},{label: "澳大利亚规则", value: 4709},{label: "加拿大规则", value: 4710}
          ],// 类型筛选预设值
          searchVipStatusList: [
            {label: "付费状态", value: -1},
            {label: "免费", value: 0},
            {label: "付费", value: 1}
          ],// 付费状态预设项
          searchStatusList: [
            {label: "发布状态", value: -1},
            {label: "下架", value: 0},
            {label: "发布", value: 1}
          ],// 发布状态预设项
          loading: false,// 登录LOAD图
        },// 页面配置数据
        form:{
          queryScope: 1,
          searchTitle: "",
          searchClass: -1,
          searchType: -1,
          searchVipStatus: "",
          searchStatus: "",
          pageNum: 1,// 表单--页码
          pageSize: 12,// 表单--页面
          total: 0,// 表单--总量
          selection: []// 多选的内容
        },// 页面表单
        formRules: {
        },// 表单验证
        responseData: {
          tableList: [],// 页面表格数据
        },// 接口响应数据
      }
    },
    //监听属性 类似于data概念
    computed: {},
    //监控data中的数据变化
    watch: {},
    //方法集合
    methods: {
      // 事件函数
      handlePageInit(){
        this.form.pageNum = 1;
        this.pageData.loading = true;// 创建加载窗口
        this.loadCmsArticleList();// 读取路由角色列表
      },// 页面初始化事件
      handleCurrentChange(val){
        this.form.pageNum = val;// 更换页码
        this.pageData.loading = true;// 创建加载窗口
        this.loadCmsArticleList();// 读取路由角色列表
      },// 页码切换事件
      handleSizeChange(val){
        this.form.pageSize = val;// 更换页面大小
        this.pageData.loading = true;// 创建加载窗口
        this.loadCmsArticleList();// 读取路由角色列表
      },// 页面size切换事件
      handleTableSelection(val){
        let list = [];// 初始化多选数组
        for (let i=0; i<val.length; i++){
          list.push(val[i].articleId);
        }// 遍历多选的列表，将ID拼入数组中
        this.form.selection = list;// 进行多选值的切换
      },// 表格多选事件
      handleSearchClass(){
        // 进行分类数据的判定
        if (this.form.searchClass !== -1){
          if (this.form.searchClass === '4'){
            // 法律法规版块，二级菜单分类为4
            this.pageData.searchTypeStatus = false;
            this.form.searchType = -1;
          } else {
            // 案例点评版块，没有二级筛选菜单，禁止菜单选择，将二级选项设为空
            this.pageData.searchTypeStatus = true;
            this.form.searchType = -1;
          }
        } else {
          this.pageData.searchTypeStatus = true;
          this.form.searchType = -1;
        }
        this.handlePageInit();
      },// 一级分类筛选事件
      handleOpenSavePage(){
        if (PermissionsToDetectFormUser(getPermission(), "400001")){
          this.$jump.push({path: "/content/articleSave", query: {routerType: 1}});
        }  else {
          this.$message.warning('您的权限不足，无法使用此功能，请联系管理员');
        }
      },// 打开新增页面
      handleOpenEditPage(scope){
        if (PermissionsToDetectFormUser(getPermission(), "400003")){
          let routeUrl = this.$router.resolve({
            path: "/content/articleEdit",
            query: {articleId: scope.row.articleId, routerType: 1}
          });
          window.open(routeUrl.href, '_blank');// 使用开启新窗口跳转
        }  else {
          // 当前权限不足，不执行操作，给与相应提示
          this.$message.warning('您的权限不足，无法使用此功能，请联系管理员');
        }
      },// 打开修改页面
      handleBatchReleaseData(){
        if (PermissionsToDetectFormUser(getPermission(), "400005")){
          this.$confirm("是否发布当前所选数据?", "提示", {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            let param = {
              articleId: this.form.selection.toString(),
              articleStatus: 1
            };
            this.batchReleaseCmsArticleData(param);
          }).catch(close => {
            console.log(close);
            this.$message.info("已取消操作");
          });
        } else {
          this.$message.warning('您的权限不足，无法使用此功能，请联系管理员');
        }
      },// 批量发布数据
      handleUpdateDataStatus(scope, status){
        if (PermissionsToDetectFormUser(getPermission(), "400005")){
          this.$confirm("是否更改当前所选数据的发布状态?", "提示", {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            let param = {
              articleId: scope.row.articleId,
              articleStatus: status
            };
            this.batchReleaseCmsArticleData(param);
          }).catch(close => {
            console.log(close);
            this.$message.info("已取消操作");
          });
        } else {
          this.$message.warning('您的权限不足，无法使用此功能，请联系管理员');
        }
      },// 更改内容发布状态
      handleDeleteData(scope){
        if (PermissionsToDetectFormUser(getPermission(), "400002")){
          // 付费内容--并且点击量不为0的内容无法删除
          if (scope.row.articleIsVip === 1 || parseInt(scope.row.articleClickCount) > 0 || scope.row.articleStatus === 1){
            this.$message.info("付费、发布、点击量大于0的内容暂不支持自动删除，请联系管理员");
          } else {
            // 进行文案内容的删除
            this.$confirm('是否删除当前所选数据?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
              // 拼接访问参数
              let prams = {articleId: scope.row.articleId,};
              // 进行数据请求
              this.deleteCmsArticleData(prams);
            }).catch(cancel => {
              console.log(cancel);
              this.$message.info("已取消操作");
            });
          }
        }  else {
          // 当前权限不足，不执行操作，给与相应提示
          this.$message.warning('您的权限不足，无法使用此功能，请联系管理员');
        }
      },// 删除内容
      // 接口
      loadCmsArticleList(){
        loadCmsArticleList(this.form).then(response => {
          if (response.code === "200"){
            this.pageData.loading = false;
            this.responseData.tableList = response.data.list;
            this.form.total = response.data.total;
          } else {
            this.$message.warning(response.message);
          }
        }).catch( error =>{
          console.log(error);
          this.$message.error(error);
        })
      },// 获取文章列表数据
      batchReleaseCmsArticleData(param){
        batchReleaseCmsArticleData(param).then(response => {
          if (response.code === "200"){
            this.$message.success(param.articleStatus === 1 ? "发布成功" : "下架成功");
            this.handlePageInit();
          } else {
            this.$message.warning(response.message);
          }
        }).catch(close => {
          console.log(close);
        });
      },// 批量发布内容
      deleteCmsArticleData(param){
        deleteCmsArticleData(param).then(response => {
          if (response.code === "200"){
            this.$message.success("删除成功");
            this.handlePageInit();
          } else {
            this.$message.warning(response.message);
          }
        }).catch(close => {
          console.log(close);
        });
      }// 删除文章数据
    },
    //生命周期 - 创建完成（可以访问当前this实例）
    created: function(){
      let _this = this;
      document.onkeydown = function(e) {
        if (!_this.pageData.loading) {
          let key = window.event.keyCode;
          if (key === 13) {
            _this.handlePageInit(); //此方法是当按下enter键后要做的动作。
          }
        }
      };
      setTimeout(function () {
        _this.handlePageInit();
      }, 800);
    },
    //生命周期 - 挂载完成（可以访问DOM元素）
    mounted() {},
    beforeCreate() {}, //生命周期 - 创建之前
    beforeMount() {}, //生命周期 - 挂载之前
    beforeUpdate() {}, //生命周期 - 更新之前
    updated() {}, //生命周期 - 更新之后
    beforeDestroy() {}, //生命周期 - 销毁之前
    destroyed() {}, //生命周期 - 销毁完成
    activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
  }
</script>

<style scoped>

</style>
